<html><head><title>NetworkServer.pb</title></head>
<body bgcolor="#FFFFDF" link="#009999" vlink="#006666" alink="#006666">
<pre><font face="Courier New, Courier, mono"size="2"><font color="#006666">;
</font><font color="#006666">; ------------------------------------------------------------
</font><font color="#006666">;
</font><font color="#006666">;   PureBasic - Network (Server) example file
</font><font color="#006666">;
</font><font color="#006666">;    (c) Fantaisie Software
</font><font color="#006666">;
</font><font color="#006666">; ------------------------------------------------------------
</font><font color="#006666">;
</font>
<b><font color="#006666">If</font></b> <font color="#006666">InitNetwork</font>() = 0
<font color="#006666">  MessageRequester</font>("Error", "Can't initialize the network !", 0)
  <b><font color="#006666">End</font></b>
<b><font color="#006666">EndIf</font></b>

Port = 6832
*Buffer =<font color="#006666"> AllocateMemory</font>(1000)

<b><font color="#006666">If</font></b> <font color="#006666">CreateNetworkServer</font>(0, Port)

<font color="#006666">  MessageRequester</font>("PureBasic - Server", "Server created (Port "+<font color="#006666">Str</font>(Port)+").", 0)
  
  <b><font color="#006666">Repeat</font></b>
      
    SEvent =<font color="#006666"> NetworkServerEvent</font>()
  
    <b><font color="#006666">If</font></b> SEvent
    
      ClientID =<font color="#006666"> EventClient</font>()
  
      <b><font color="#006666">Select</font></b> SEvent
      
        <b><font color="#006666">Case</font></b> <font color="#924B72">#PB_NetworkEvent_Connect</font>
<font color="#006666">          MessageRequester</font>("PureBasic - Server", "A new client has connected !", 0)
  
        <b><font color="#006666">Case</font></b> <font color="#924B72">#PB_NetworkEvent_Data</font>
<font color="#006666">          MessageRequester</font>("PureBasic - Server", "Client "+<font color="#006666">Str</font>(ClientID)+" has send a packet !", 0)
<font color="#006666">          ReceiveNetworkData</font>(ClientID, *Buffer, 1000)
<font color="#006666">          MessageRequester</font>("Info", "String: "+<font color="#006666">PeekS</font>(*Buffer, -1, <font color="#924B72">#PB_UTF8</font>), 0)
  
        <b><font color="#006666">Case</font></b> <font color="#924B72">#PB_NetworkEvent_Disconnect</font>
<font color="#006666">          MessageRequester</font>("PureBasic - Server", "Client "+<font color="#006666">Str</font>(ClientID)+" has closed the connection...", 0)
          Quit = 1
    
      <b><font color="#006666">EndSelect</font></b>
    <b><font color="#006666">EndIf</font></b>
    
  <b><font color="#006666">Until</font></b> Quit = 1 
  
<font color="#006666">  MessageRequester</font>("PureBasic - Server", "Click to quit the server.", 0)
  
<font color="#006666">  CloseNetworkServer</font>(0)
<b><font color="#006666">Else</font></b>
<font color="#006666">  MessageRequester</font>("Error", "Can't create the server (port in use ?).", 0)
<b><font color="#006666">EndIf</font></b>

  
<b><font color="#006666">End</font></b>   

</font></pre>
</body></html>
